package com.lora.service.resolver;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.lora.dto.EquipmentDto;
import com.lora.entity.SwitchingValue;
import com.lora.service.NodeDataResolver;
import com.lora.service.SwitchingValueService;
import com.lora.web.BaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;
import java.util.Iterator;

@Slf4j
@Component("switchingValueResolver")
public class SwitchingValueResolver implements NodeDataResolver<SwitchingValue> {

    @Resource
    private SwitchingValueService switchingValueService;

    @Override
    public EquipmentDto<SwitchingValue> resolve(String equipmentNumber, String data) {
        data = BaseUtil.base64ToHex(data);
        EquipmentDto<SwitchingValue> equipmentDto = new EquipmentDto<>();
        SwitchingValue switchingValue = new SwitchingValue();
        switchingValue.setEquipmentid(equipmentNumber);
        switchingValue.setDatastring(data);
        // data = "04032301031000110000000100030000000100011712c6a4";

        if("ffffff100000f10f".equals(equipmentNumber)) { // AD88
            String diData = data.substring(12, data.length() - 4);
            Iterable<String> split = Splitter.fixedLength(4).split(diData);
            Iterator<String> iterator = split.iterator();
            int index = 1;
            while(iterator.hasNext()) {
                String binaryDi = Strings.padStart(Integer.toBinaryString(Integer.parseInt(iterator.next(), 10)), 8, '0');
                if(index == 1) {
                    switchingValue.setDi1(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 2) {
                    switchingValue.setDi2(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 3) {
                    switchingValue.setDi3(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 4) {
                    switchingValue.setDi4(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 5) {
                    switchingValue.setDi5(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 6) {
                    switchingValue.setDi6(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 7) {
                    switchingValue.setDi7(Integer.parseInt(binaryDi, 2) + "");
                } else if(index == 8) {
                    switchingValue.setDi8(Integer.parseInt(binaryDi, 2) + "");
                }
                index++;
            }
        } else {
            String diData = data.substring(12, 16);
            String doData = data.substring(16, 20);
            String binaryDi = Strings.padStart(Integer.toBinaryString(Integer.parseInt(diData, 16)), 8, '0');
            String binaryD0 = Strings.padStart(Integer.toBinaryString(Integer.parseInt(doData, 16)), 8, '0');

            switchingValue.setDi1(binaryDi.substring(7));
            switchingValue.setDi2(binaryDi.substring(6,7));
            switchingValue.setDi3(binaryDi.substring(5,6));
            switchingValue.setDi4(binaryDi.substring(4,5));
            switchingValue.setDi5(binaryDi.substring(3,4));
            switchingValue.setDi6(binaryDi.substring(2,3));
            switchingValue.setDi7(binaryDi.substring(1,2));
            switchingValue.setDi8(binaryDi.substring(0,1));

            switchingValue.setDo1(binaryD0.substring(7));
            switchingValue.setDo2(binaryD0.substring(6,7));
            switchingValue.setDo3(binaryD0.substring(5,6));
            switchingValue.setDo4(binaryD0.substring(4,5));
            switchingValue.setDo5(binaryD0.substring(3,4));
            switchingValue.setDo6(binaryD0.substring(2,3));
            switchingValue.setDo7(binaryD0.substring(1,2));
            switchingValue.setDo8(binaryD0.substring(0,1));
        }

        equipmentDto.setEntity(switchingValue);
        return equipmentDto;
    }

    @Override
    public SwitchingValue save(SwitchingValue equipment, boolean alarmFlag) {
        equipment.setAlarmFlag(alarmFlag);
        switchingValueService.save(equipment);
        return equipment;
    }

    public static void main(String[] args) {

        System.out.println(Strings.padStart(Integer.toBinaryString(Integer.parseInt("14", 10)), 8, '0'));


        String data = "04032301031000110000000100030000000100011712c6a4";
        String diData = data.substring(12, 16);
        String doData = data.substring(16, 20);
        System.out.println(diData);
        System.out.println(doData);

        System.out.println(Integer.parseInt(diData, 10));
        System.out.println(Strings.padStart(Integer.toBinaryString(11), 8, '0'));


        System.out.println(Base64Utils.encodeToString("FE 01 03 00 01 00 38 15 d8".getBytes()));
        System.out.println(new String(Base64Utils.decodeFromString("XHgwMVx4MDFceDAxXHgwMQ")));

    }

}
